package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.article.ArticleHistoryHandler;
import com.prp.gwt.client.user.article.ArticleListener;
import com.prp.gwt.client.vo.SubjectCategory;
import com.prp.gwt.client.vo.SubjectChildCategory;
import com.prp.gwt.client.vo.SubjectChildCategoryList;

public class SideBarCategory extends VerticalPanel {

	public boolean flag = false;
	public FlowPanel panel = new FlowPanel();
	
	public SideBarCategory(final SubjectCategory category) {
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.addStyleName("article-sidebar-category");
			HTML name = new HTML("<a>"+category.getName()+"</a>");
			name.addStyleName("article-sidebar-category-text");
			hPanel.add(name);
			HTML link = new HTML("<a>创建实体</a>");
			hPanel.add(link);
			link.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(ArticleHistoryHandler.PREFIX_CREATE_ENTITY+category.getId());
				}
			});
			hPanel.setCellHorizontalAlignment(link, ALIGN_RIGHT);
			add(hPanel);
			name.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (!flag)
						ArticleListener.getInstance().getSubjectChildCategoryList(
								category.getId(), SideBarCategory.this);
					else
						panel.setVisible(!panel.isVisible());
				}
			});
		}
		add(panel);
		panel.addStyleName("article-sidebar-childPanel");
	}

	public void load(SubjectChildCategoryList childList) {
		flag = true;
		for(int i = 0; i < childList.size(); i++){
			final SubjectChildCategory scc = childList.get(i);
			HTML child = new HTML("<a>"+scc.getName()+"</a>");
			child.addStyleName("article-sidebar-child");
			panel.add(child);
			child.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					ArticleListener.getInstance().getEntityList(scc.getName(), scc.getId());
				}
			});
		}
	}

}
